{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Spiral Traversal of Grid\n",
    "\n",
    "[The Original Question](https://mp.weixin.qq.com/s/Xk8GOWrlgcCzYfo3JN54DA)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quesiton\n",
    "\n",
    "You are given a 2D array of integers. Print out the clockwise spiral traversal of the matrix. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "```python3\n",
    "grid = [\n",
    "    [1, 2, 3, 4, 5],\n",
    "    [5, 6, 7, 8, 9, 10],\n",
    "    [11, 12, 13, 14, 15],\n",
    "    [16, 17, 18, 19, 20]\n",
    "]\n",
    "```\n",
    "\n",
    "The clockwise spiral traversal of this array is:\n",
    "\n",
    "```python3\n",
    "[1, 2, 3, 4, 5, 10, 15, 20, 19, 18, 17, 16, 11, 6, 7, 8, 9, 14, 13, 12]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 9, 15, 20, 19, 18, 17, 16, 11, 5, 6, 7, 8, 14, 13, 12]\n"
     ]
    }
   ],
   "source": [
    "def matrix_spiral_print(M):\n",
    "    # Fill this in.\n",
    "    path = list()\n",
    "    # Confirm borders of the matrix.\n",
    "    up = left = 0\n",
    "    right = len(grid[0])\n",
    "    bottom = len(grid)\n",
    "    \n",
    "    # Traverse a loop each time.\n",
    "    while up < bottom and left < right:\n",
    "        # The upper border.\n",
    "        for k in range(left, right):\n",
    "            path.append(grid[up][k])\n",
    "            pass\n",
    "        up += 1\n",
    "\n",
    "        # The right border.\n",
    "        for k in range(up, bottom):\n",
    "            path.append(grid[k][right - 1])\n",
    "            pass\n",
    "        right -= 1\n",
    "\n",
    "        # The bottom border.\n",
    "        for k in range(right - 1, left - 1, -1):\n",
    "            path.append(grid[bottom - 1][k])\n",
    "            pass\n",
    "        bottom -= 1\n",
    "\n",
    "        # The left border.\n",
    "        for k in range(bottom - 1, up - 1, -1):\n",
    "            path.append(grid[k][left])\n",
    "            pass\n",
    "        left += 1\n",
    "\n",
    "        pass\n",
    "    \n",
    "    # Output result.\n",
    "    print(path)\n",
    "    pass\n",
    "\n",
    "\n",
    "grid = [\n",
    "    [1, 2, 3, 4, 5],\n",
    "    [5, 6, 7, 8, 9, 10],\n",
    "    [11, 12, 13, 14, 15],\n",
    "    [16, 17, 18, 19, 20]\n",
    "]\n",
    "matrix_spiral_print(grid)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
